package com.adou.multi.controller;

import java.util.List;
import java.util.concurrent.CompletableFuture;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.adou.multi.base_controller.AbstractBaseApiController;
import com.adou.multi.base_controller.ContentType;
import com.adou.multi.base_controller.NotFoundException;
import com.adou.multi.model.ProductValue;
import com.adou.multi.multi_tenancy.page.ErrorCode;
import com.adou.multi.multi_tenancy.page.PageResult;
import com.adou.multi.multi_tenancy.page.ResponseVo;
import com.adou.multi.service.ProductService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/")
@Api(tags = "product")
public class ProductController extends AbstractBaseApiController {
	@Autowired
	private ProductService productService;

	@GetMapping(value = "/products", produces = { ContentType.PRODUCTS_1_0 })
	public ResponseEntity<List<ProductValue>> getProducts() {
		List<ProductValue> productValues = productService.getProducts();
		return new ResponseEntity<List<ProductValue>>(productValues, HttpStatus.OK);
	}

	@GetMapping(value = "/products/{id}", produces = { ContentType.PRODUCT_1_0 })
	public ResponseEntity<ProductValue> getProduct(@PathVariable("id") long id) {
		try {
			ProductValue branch = productService.getProduct(id);
			return new ResponseEntity<>(branch, HttpStatus.OK);
		} catch (Exception e) {
			throw new NotFoundException(e.getMessage());
		}
	}

	@PostMapping(value = "products", consumes = { ContentType.PRODUCT_1_0 }, produces = { ContentType.PRODUCT_1_0 })
	public ResponseEntity<ProductValue> createProduct(@Valid @RequestBody ProductValue productValue) {
		ProductValue product = productService.createProduct(productValue);
		MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
		headers.add(HttpHeaders.LOCATION, "products" + product.getId());
		return new ResponseEntity(product, headers, HttpStatus.CREATED);
	}

	@PutMapping(value = "/products/{id}", consumes = { ContentType.PRODUCT_1_0 }, produces = {
			ContentType.PRODUCT_1_0 })
	ResponseEntity<ProductValue> updateProduct(@PathVariable long id, @Valid @RequestBody ProductValue productValue) {
		productValue.setId(id);
		try {
			ProductValue product = productService.updateProduct(productValue);
			return new ResponseEntity<>(product, HttpStatus.OK);
		} catch (Exception e) {
			throw new NotFoundException(e.getMessage());
		}

	}

	@DeleteMapping("products/{id}")
	ResponseEntity<Void> deleteProduct(@PathVariable int id) {
		try {
			productService.deleteProductById(id);
			return new ResponseEntity<>(null, HttpStatus.NO_CONTENT);
		} catch (Exception e) {
			throw new NotFoundException(e.getMessage());
		}
	}

	@GetMapping(value = "/async/products", produces = { ContentType.PRODUCTS_1_0 })
	public CompletableFuture<ResponseEntity<List<ProductValue>>> aynecGetProducts() {
		List<ProductValue> productValues = productService.getProducts();
		return CompletableFuture.completedFuture(new ResponseEntity<>(productValues, HttpStatus.OK));

	}

	@GetMapping(value = "/async/products/{id}", produces = { ContentType.PRODUCT_1_0 })
	public CompletableFuture<ResponseEntity<ProductValue>> asyncGetProduct(@PathVariable("id") long id) {
		return CompletableFuture.completedFuture(getProduct(id));
	}

	@ApiOperation("分页查询")
	@GetMapping("page")
	public ResponseVo<String> qryProductPage(@RequestParam(required = false) String name,
			@RequestParam(required = false, defaultValue = "1") Integer pageNumber,
			@RequestParam(required = false, defaultValue = "2") Integer pageSize) {

		ProductValue dto = ProductValue.builder().name(name).build();

		PageResult<ProductValue> result = productService.qryProductValuePage(pageNumber, pageSize, null, null, dto);
		ResponseVo.Page pageVo = new ResponseVo.Page(pageNumber, pageSize, (int) result.getTotalElements());

		return result.getContent() != null ? ResponseVo.success(result.getContent(), pageVo)
				: ResponseVo.failtrue(ErrorCode.SELECT_ERROR, "我发获取产品信息");

	}

}
